﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class GoodsChart_two : Form
    {
        public GoodsChart_two()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            cbotype.SelectedIndex = 0;
            this.Opacity = 0;
        }

        private void btnselect_Click(object sender, EventArgs e)
        {
            sousuo();
        }

        private void GoodsChart_two_Load(object sender, EventArgs e)
        {
            this.dtpqishi.Value = DateTime.Now.AddYears(-1);
            this.timer1.Start();
            btnselect_Click(sender, e);
        }
        public void sousuo() {
            if (this.cbotype.Text == "按类型")
            {
                memberInfo mi = new memberInfo();
                DataSet dt2 = mi.GetListSql("select aa.typeName as 类型,sum(aa.金额) as 销售额 from (select gt.typeName,sum(abc.totalcount) as 金额,gid from (select gid,goodsprice,SUM(goodscount) sumgoodscount,goodsprice*SUM(goodscount)*zhekou/10 totalcount from goodsInfo where  selltime > '" + this.dtpqishi.Value.Date + "' and selltime < '" + this.dtpjiezhi.Value.Date + "'  group by gid,goodsprice,zhekou) as abc inner join dbo.goods g on(g.goodsId = gid) inner join dbo.goodsType gt on(gt.id = g.goodsTypeId) group by gid,gt.typeName) as aa group by aa.typeName");
                decimal zonghe = 0;
                for (int i = 0; i < dt2.Tables[0].Rows.Count; i++)
                {
                    zonghe = zonghe + decimal.Parse(dt2.Tables[0].Rows[i]["销售额"].ToString());
                }
                List<string> name = new List<string>();
                List<string> bili = new List<string>();
                for (int i = 0; i < dt2.Tables[0].Rows.Count; i++)
                {


                    string aa = ((100 / zonghe) * decimal.Parse(dt2.Tables[0].Rows[i]["销售额"].ToString())).ToString();

                    bili.Add(aa);
                    name.Add(dt2.Tables[0].Rows[i]["类型"].ToString() + " 比例：" + decimal.Parse(aa).ToString("F1") + "%");

                }
                chart1.Series["Series1"]["PieLabelStyle"] = "Outside";
                chart1.Series["Series1"]["PieLinelColor"] = "Red";
                chart1.Series[0].Points.DataBindXY(name, bili);
                chart1.DataBind();
            }
            if (this.cbotype.Text == "按商品")
            {
                memberInfo mi = new memberInfo();
                DataSet dt2 = mi.GetListSql("select aa.goodsName as 商品,sum(aa.金额) as 销售额 from(   select g.goodsName,sum(abc.totalcount) as 金额,gid from      (         select gid,goodsprice,SUM(goodscount) sumgoodscount,goodsprice*SUM(goodscount)*zhekou/10 totalcount from goodsInfo          where  selltime > '" + this.dtpqishi.Value.Date + "' and selltime < '" + this.dtpjiezhi.Value.Date + "'           group by gid,goodsprice,zhekou     ) as abc          inner join dbo.goods g on(g.goodsId = gid)          group by gid,g.goodsName      ) as aa group by aa.goodsName");
                decimal zonghe = 0;
                for (int i = 0; i < dt2.Tables[0].Rows.Count; i++)
                {
                    zonghe = zonghe + decimal.Parse(dt2.Tables[0].Rows[i]["销售额"].ToString());
                }
                List<string> name = new List<string>();
                List<string> bili = new List<string>();
                for (int i = 0; i < dt2.Tables[0].Rows.Count; i++)
                {


                    string aa = ((100 / zonghe) * decimal.Parse(dt2.Tables[0].Rows[i]["销售额"].ToString())).ToString();

                    bili.Add(aa);
                    name.Add(dt2.Tables[0].Rows[i]["商品"].ToString() + " 比例：" + decimal.Parse(aa).ToString("F1") + "%");

                }
                chart1.Series["Series1"]["PieLabelStyle"] = "Outside";
                chart1.Series["Series1"]["PieLinelColor"] = "Red";
                chart1.Series[0].Points.DataBindXY(name, bili);
                chart1.DataBind();
            }
        
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;

        private void GoodsChart_two_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }
        

       
    }
}
